package client;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class NetworkDialog extends JDialog implements ActionListener{
	private JLabel serverIPLabel = new JLabel("Server IP:");
	private JLabel serverPortLabel = new JLabel("Port:");
	private JLabel choiceLabel = new JLabel("Remember my choice");
	
	private JTextField serverIPField = new JTextField();
	private JTextField serverPortField= new JTextField();
	
	private JCheckBox checkBox = new JCheckBox();
	
	private JButton okButton = new JButton("OK");
	private JButton cancelButton = new JButton("Cancel");
	
	private String serverIP = "";
	private String serverPort = "";
	
	ActionListener listener;
	
	Properties properties;
	
		
	public String getServerIP() {
		return serverIP;
	}
	public void setServerIP(String serverIP) {
		this.serverIP = serverIP;
	}
	public String getServerPort() {
		return serverPort;
	}
	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}
	
	public NetworkDialog(Properties properties){
		this.properties = properties;
		this.serverIP = this.properties.getProperty("server_url");
		this.serverPort = this.properties.getProperty("port");
		
		Container cp = this.getContentPane();

		this.setSize(400, 300);
		this.setResizable(false);
		
		setTitle("Configure connection settings");
		setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);	
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		
		cp.setLayout(gbl);
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = GridBagConstraints.REMAINDER;
		this.addComponent(cp, serverIPLabel, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, serverIPField, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		this.addComponent(cp, serverPortLabel, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, serverPortField, gbl, gbc);
		
				
		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.gridwidth = 1;
		gbc.weightx = GridBagConstraints.RELATIVE;
		gbc.insets = new Insets(0, 0, 0, 0);
		this.addComponent(cp, choiceLabel, gbl, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbc.weightx = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, checkBox, gbl, gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy = 6;
		gbc.weightx = 0.5;
		gbc.insets = new Insets(15, 5, 15, 5);
		this.addComponent(cp, okButton, gbl, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 6;
		gbc.weightx = 0.5;
		this.addComponent(cp, cancelButton, gbl, gbc);
		
		okButton.setActionCommand("select");
		cancelButton.setActionCommand("dbselection_cancelled");
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		serverIPField.setText(serverIP);
		serverPortField.setText(serverPort);
		
		this.setModal(true);
	}
	protected void addComponent (Container cp, Component comp, GridBagLayout gridbag, GridBagConstraints gbc) {
		gridbag.setConstraints(comp, gbc);
		cp.add(comp);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == "select"){
			properties.setProperty("server_url", serverIPField.getText());
			properties.setProperty("port", serverPortField.getText());
			if(checkBox.isSelected())
				properties.writePropertiesInFile("configs/client/client_conf.xml");
		}
		this.setVisible(false);
	}
	public Properties getProperties(){
		return properties;
	}
}